#pragma once
#include<iostream>
#include<sys/types.h>
#include<unistd.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<sys/socket.h>
#include<string>
#include<cstring>
class InetAddr
{
    public:
    //网络转主机
    InetAddr(struct sockaddr_in addr):_addr(addr)
    {
        char buffer[1024];
        inet_ntop(AF_INET,&_addr.sin_addr,buffer,sizeof(buffer));
        _port=ntohs(_addr.sin_port);

        _ip=buffer;
    }

    //主机包装成网络：
    InetAddr(std::string ip,uint16_t port):_ip(ip),_port(port)
    {
        memset(&_addr,0,sizeof(_addr));
        //第三个参数是存储二进制ip地址的缓冲区
        inet_pton(AF_INET,_ip.c_str(),&_addr.sin_addr);
        _addr.sin_family=AF_INET;
        _addr.sin_port=htons(_port);
    }
    std::string Ip()
    {
        return _ip;
    }
    uint16_t Port()
    {
        return _port;
    }
    private:
    std::string _ip;
    uint16_t _port;
    struct sockaddr_in _addr;
};

